import React, { Component } from 'react'
import {
  BrowserRouter as Router,
  Switch,
  Route,
  Redirect
} from 'react-router-dom'
import App from './App'
import { NotFound, Login, Register } from './components'
import { connect } from 'react-redux'
const mapState = state => {
  return {
    hasLogin: state.user.haslogin
  }
}
@connect(mapState)
export default class routeComponents extends Component {
  render() {
    return (
      <Router>
        <Switch>
          <Route
            path="/admin"
            render={appProps => {
              if (this.props.hasLogin) {
                return <App {...appProps} />
              } else {
                return <Redirect to="/login" />
              }
            }}
          />
          <Route path="/login" component={Login} exact />
          <Route path="/register" component={Register} exact />
          <Route path="/404" component={NotFound} />
          <Redirect to="admin" from="/" exact />
          <Redirect to="/404" />
        </Switch>
      </Router>
    )
  }
}
